// packageH/sign_shopV2/component/person_bank/person_bank.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    hasCompany: {
      type: Boolean,
      value: false
    },
    real_name_auth_set: {
      type: Object,
      value: {}
    },
  },
  observers: {
    'show': function(valA,valB) {
      this.setData({
        showPersonBank: valA
      })
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    showPersonBank: false,
    activeName: "1",
    form: {
      name: "",
      id_no: "",
      tel: "",
      face_auth_mode: "",
    },
    authcode: "",
    bank_no: "",
    flow_id: "",
    count: "",
    timer: null,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 表单修改
    handlerInput(event) {
      let type = event.target.dataset.type;
      this.setData({
        [`${type}`]: event.detail,
      });
    },
    // 获取短信验证码
    sureIdentify() {
      if (this.data.activeName == 2 && !this.data.bank_no) {
        app.tips("请先填写银行卡");
        return;
      }
      if (!this.data.form.tel) {
        app.tips("请先填写手机号");
        return;
      }
      let queryUrl = "";
      let json = {};
      const TIME_COUNT = 60;
      if (this.data.activeName == 1) {
        queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.h5.person.auth-tel-v2");
        json = this.data.form;
      } else if (this.data.activeName == 2) {
        queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.h5.person.auth-bank-card-v2");
        this.setData({
          ['form.bank_no']: this.data.bank_no
        });
        json = this.data.form;
      }
      app._postNetWork({
        url: queryUrl,
        data: json,
        success:(resdata)=> {
          this.data.timer = null;
          if (resdata.data.result === 1) {
            let res = resdata.data;
            app.tips("已发送");
            this.setData({
              flow_id: res.data.data.flowId,
            });
            if (!this.data.timer) {
              this.countDown = TIME_COUNT;
              this.data.timer = setInterval(() => {
                if (this.countDown > 0 && this.countDown <= TIME_COUNT) {
                  this.setData({
                    count: --this.countDown,
                  });
                } else {
                  clearInterval(this.data.timer);
                  that.data.timer = null;
                }
              }, 1000);
            }
          } else {
            wx.showModal({
              title: "提示",
              content: resdata.data.msg,
              showCancel: false,
            });
          }
        },
      });
    },
    sendCode() {
      if (this.data.activeName == 2 && !this.data.bank_no) {
        app.tips("请先填写银行卡");
        return;
      }
      if (this.data.activeName != 3 && !this.data.flow_id) {
        app.tips("请先获取验证码");
        return;
      }
      console.log(this.data.authcode);
      if (this.data.activeName != 3 && !this.data.authcode) {
        app.tips("请先填写验证码");
        return;
      }
      if (this.data.activeName == 3 && !this.data.form.face_auth_mode) {
        console.log(typeof this.data.activeName, this.data.form);
        app.tips("请先选择识别方式");
        return;
      }
     
      this.sendAuthRequest();
    },
    sendAuthRequest() {
      let url = "";
      let json = {};
      if (this.data.activeName == 1) {
        url = app.getNetAddresss("plugin.yun-sign.frontend.person.check-tel-sms-code");
        json = {
          flow_id: this.data.flow_id,
          authcode: this.data.authcode
        };
      } else if (this.data.activeName == 2) {
        url = app.getNetAddresss("plugin.yun-sign.frontend.person.check-bank-sms-code");
        json = {
          flow_id: this.data.flow_id,
          authcode: this.data.authcode
        };
      } else {
        url = app.getNetAddresss("plugin.yun-sign.frontend.h5.person.auth-face-v2");
        json = {
          name: this.data.form.name,
          id_no: this.data.form.id_no,
          tel: this.data.form.tel,
          face_auth_mode: this.data.form.face_auth_mode,
          callback_url: ''
        };
      }
  
      app._postNetWork({
        url,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          if (res.result === 1) {
            app.tips(res.msg);
            this.triggerEvent('update')
            this.hiddenPop();
          } else {
            wx.showModal({
              title: "提示",
              content: resdata.data.msg,
              showCancel: false
            });
          }
        },
        fail(err) {
          app.tips(err.msg);
        }
      });
    },
    hiddenPop(){
      this.setData({
        showPersonBank: false
      })
    }
  }
})